package com.wlt.controller;

import com.wlt.pojo.GoodsDesc;
import com.wlt.pojo.GoodsSearchParam;
import com.wlt.pojo.GoodsSearchResult;
import com.wlt.result.BaseResult;
import com.wlt.service.GoodsService;
import com.wlt.service.SearchService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 商品搜索
 */
@RestController
@RequestMapping("/user/goodsSearch")
public class GoodsSearchController
{
    @DubboReference
    private SearchService searchService;
    @DubboReference
    private GoodsService goodsService;
    
    /**
     * 自动补齐关键字的功能
     * @param keyword       被补齐的关键字
     * @return 补齐的关键字集合
     * @throws IOException  IO异常
     */
    @GetMapping ("/autoSuggest")
    public BaseResult<List<String>> autoSuggest (String keyword) throws IOException
    {
        List<String> strings = searchService.autoSuggest(keyword);
        
        return BaseResult.success(strings);
    }
    
    /**
     * 搜索商品
     * @param goodsSearchParam  搜索条件
     * @return                  搜索结果
     */
    @PostMapping ("/search")
    public BaseResult<GoodsSearchResult> search (@RequestBody GoodsSearchParam goodsSearchParam)
    {
        GoodsSearchResult search = searchService.search(goodsSearchParam);
        
        return BaseResult.success(search);
    }
    
    /**
     * 根据id查询商品详情
     * @param id    商品id
     * @return      商品详情
     */
    @GetMapping("/findDesc")
    public BaseResult<GoodsDesc> findDesc (Long id)
    {
        GoodsDesc desc = goodsService.findDesc(id);
        
        return BaseResult.success(desc);
    }
}
